package com.yuyang.wifi.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.yuyang.wifi.MyApplication;
import com.yuyang.wifi.utils.DeviceUtil;
import com.qq.e.ads.nativ.express2.AdEventListener;
import com.qq.e.ads.nativ.express2.MediaEventListener;
import com.qq.e.ads.nativ.express2.NativeExpressAD2;
import com.qq.e.ads.nativ.express2.NativeExpressADData2;
import com.qq.e.comm.util.AdError;

import java.util.List;

/**
 * created by mej
 * on 2020/12/2
 */
public class LoadYlhMBMsgAdUtils implements
        View.OnClickListener, NativeExpressAD2.AdLoadListener {

    private Context mContext;
    private OnItemAllClickListener onItemAllClickListener;
    private OnItemCloseListener onItemCloseListener;
    private NativeExpressAD2 mNativeExpressAD2;
    private static final String TAG = "ylh_mb_msg";
    private NativeExpressADData2 mNativeExpressADData2;

    static LoadYlhMBMsgAdUtils instance = new LoadYlhMBMsgAdUtils();
    private FrameLayout mAdContainer;
    private String mYlhMsgAdID;

    public static LoadYlhMBMsgAdUtils getInstance() {
        return instance;
    }
    private OnLoadFailureListener onLoadFailureListener;
    //一、加载穿山甲激励视频
    //BDAdvanceBaseAppNative 绑定激励视频回调
    public void load(final Context context, FrameLayout adContainer, String moduleName) {
        mContext = context;
        mAdContainer = adContainer;//3091718613719724
        mYlhMsgAdID = MyApplication.getYlhChannel(moduleName);
        mNativeExpressAD2 = new NativeExpressAD2(mContext, mYlhMsgAdID, this);
    }

    public void showInterActionAdVideo() {
//        String  streamStatus = SpUtil.find(AppConstant.KEY_STREAM_STATUS);
//        if(streamStatus!=null&&!streamStatus.isEmpty()&&streamStatus.equals("0")) {

        // 注意不要填超过Integer.MAX_VALUE的值，这里暂且 catch 相关异常
        try {
            int screenWidth = DeviceUtil.getWidth(mContext);
            int scree = DeviceUtil.px2dp(mContext, screenWidth);
            //int scree = DeviceUtil.px2dp(mContext, screenWidth)-80;
            mNativeExpressAD2.setAdSize(scree, 0);
            // 如果您在平台上新建原生模板广告位时，选择了支持视频，那么可以进行个性化设置（可选）
            // mNativeExpressAD2.setVideoOption2(null);
            mNativeExpressAD2.loadAd(1);
//                destroyAd();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        }
    }

    /**
     * 广告加载成功回调
     *
     * @param adDataList
     */
    @Override
    public void onLoadSuccess(List<NativeExpressADData2> adDataList) {
        Log.i(TAG, "onLoadSuccess: size " + adDataList.size());
        renderAd(adDataList);
    }

    /**
     * 渲染广告
     *
     * @param adDataList
     */
    private void renderAd(List<NativeExpressADData2> adDataList) {
        if (adDataList.size() > 0) {
            if (null != mAdContainer) {
                mAdContainer.removeAllViews();
            }
            mNativeExpressADData2 = adDataList.get(0);
            Log.i(TAG, "renderAd: " + "  eCPM level = " +
                    mNativeExpressADData2.getECPMLevel() + "  Video duration: " + mNativeExpressADData2.getVideoDuration());
            mNativeExpressADData2.setAdEventListener(new AdEventListener() {
                @Override
                public void onClick() {
                    Log.i(TAG, "onClick: " + mNativeExpressADData2);
                }

                @Override
                public void onExposed() {
                    Log.i(TAG, "onImpression: " + mNativeExpressADData2);
                }

                @Override
                public void onRenderSuccess() {
                    try {
                        if (null != mAdContainer) {

                            mAdContainer.removeAllViews();
                            if (mNativeExpressADData2.getAdView() != null) {
                                mAdContainer.addView(mNativeExpressADData2.getAdView());
                            }
                            if (null != onItemAllClickListener) {
                                onItemAllClickListener.onItemAllClickListener();
                            }
//                            int visibility = mAdContainer.getVisibility();
//                            if (visibility != 0) {
//                                mAdContainer.setVisibility(View.VISIBLE);
//                                Log.i(TAG, "onRenderSuccess: " + mNativeExpressADData2);
//                                mAdContainer.removeAllViews();
//                                if (mNativeExpressADData2.getAdView() != null) {
//                                    mAdContainer.addView(mNativeExpressADData2.getAdView());
//                                }
//                                if (null != onItemAllClickListener) {
//                                    onItemAllClickListener.onItemAllClickListener();
//                                }
//                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onRenderFail() {
                    Log.i(TAG, "onRenderFail: " + mNativeExpressADData2);
                }

                @Override
                public void onAdClosed() {
                    Log.i(TAG, "onAdClosed: " + mNativeExpressADData2);
                    if (null != mAdContainer) {
                        mAdContainer.removeAllViews();
                    }
                    mNativeExpressADData2.destroy();
                    if (null != onItemCloseListener) {
                        onItemCloseListener.onItemCloseListener();
                    }
                }
            });

            mNativeExpressADData2.setMediaListener(new MediaEventListener() {
                @Override
                public void onVideoCache() {
                    Log.i(TAG, "onVideoCache: " + mNativeExpressADData2);
                }

                @Override
                public void onVideoStart() {
                    Log.i(TAG, "onVideoStart: " + mNativeExpressADData2);
                }

                @Override
                public void onVideoResume() {
                    Log.i(TAG, "onVideoResume: " + mNativeExpressADData2);
                }

                @Override
                public void onVideoPause() {
                    Log.i(TAG, "onVideoPause: " + mNativeExpressADData2);
                }

                @Override
                public void onVideoComplete() {
                    Log.i(TAG, "onVideoComplete: " + mNativeExpressADData2);
                }

                @Override
                public void onVideoError() {
                    Log.i(TAG, "onVideoError: " + mNativeExpressADData2);
                }
            });

            mNativeExpressADData2.render();
        }
    }

    /**
     * 广告加载失败回调
     *
     * @param error
     */
    @Override
    public void onNoAD(AdError error) {
        @SuppressLint("DefaultLocale")
        String errorMsg = String
                .format("onNoAD, error code: %d, error msg: %s", error.getErrorCode(), error.getErrorMsg());
        Log.i(TAG, "onNoAD: " + errorMsg+onLoadFailureListener);
        // Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();
        if (null != onLoadFailureListener) {
            onLoadFailureListener.onLoadFailureListener();
        }
    }

    private void loadAd() {
        // 注意不要填超过Integer.MAX_VALUE的值，这里暂且 catch 相关异常
        try {
            mNativeExpressAD2.setAdSize(340, 0);
            // 如果您在平台上新建原生模板广告位时，选择了支持视频，那么可以进行个性化设置（可选）
            // mNativeExpressAD2.setVideoOption2(null);
            mNativeExpressAD2.loadAd(1);
            destroyAd();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放前一个 NativeExpressAD2Data 的资源
     */
    private void destroyAd() {
        if (mNativeExpressADData2 != null) {
            Log.d(TAG, "destroyAD");
            mNativeExpressADData2.destroy();
        }
    }

    @Override
    public void onClick(View view) {

    }


    public interface OnItemAllClickListener {
        void onItemAllClickListener();
    }


    public void setItemAllClickListener(OnItemAllClickListener onItemAllClickListener) {
        this.onItemAllClickListener = onItemAllClickListener;
    }


    public interface OnItemCloseListener {
        void onItemCloseListener();
    }

    public void setItemCloseListener(OnItemCloseListener onItemCloseListener) {
        this.onItemCloseListener = onItemCloseListener;
    }


    public interface OnLoadFailureListener {
        void onLoadFailureListener();
    }

    public void setLoadFailureListener(OnLoadFailureListener onLoadFailureListener) {
        this.onLoadFailureListener = onLoadFailureListener;
    }

}
